package com.qf.logistics.security.service;

import com.qf.logistics.mapper.SysUserMapper;
import com.qf.logistics.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * created by 赵文韬 on 2022/3/30 15:36
 */
@Component
public class MyUserSetailService implements UserDetailsService {
    private SysUserMapper sysUserMapper;


    @Autowired
    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user = sysUserMapper.findUserByUserName(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户名密码不存在");
        }
        if (user.getStatus() == 0) {
            throw new LockedException("账号被封禁");
        }

        return new BaseUser(username,user.getPassword() ,new ArrayList<>() , user.getUserId());
    }
}
